<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">
          当前城市
        </div>
        <div class="button_list">
          <div class="button_warpper">
            <div class="button">
              {{this.currentCity}}
            </div>
          </div>
        </div>
      </div>
        <div class="area">
          <div class="title border-topbottom">
            热门城市
          </div>
          <div class="button_list">
            <div class="button_warpper" v-for="item of hotCities" :key="item.id">
              <div class="button" @click="handleCity(item.name)">
                {{item.name}}
              </div>
            </div>
          </div>
        </div>
        <div class="area" v-for="(item,key) of cities" :key="key">
          <div class="title border-topbottom">
            {{key}}
          </div>
          <div class="item_list" v-for="inner of item" :key="inner.id" :ref="key">
            <div class="item border-bottom" @click="handleCity(inner.name)">{{inner.name}}</div>
          </div>
        </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { mapState } from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        this.scroll.scrollToElement(this.$refs[this.letter][0])
      }
    }
  },
  methods: {
    handleCity (city) {
      this.$store.dispatch('changeCity', city)
      this.$router.push('/')
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  }
}
</script>

<style lang="stylus" scoped>
 @import '~styles/varibles.styl'
 .border-topbottom
  &:before
    border-color #ccc
  &:after
    border-color #ccc
 .border-bottom
  &:before
    border-color #ccc
 .list
  position absolute
  top 1.58rem
  left 0
  right 0
  bottom 0
  overflow hidden
  .title
   height .44rem
   padding-left .2rem
   background #eee
   line-height .44rem
   color #666
  .button_list
   padding .1rem .6rem .1rem .1rem
   overflow hidden
   .button_warpper
    float left
    width 33.3%
    .button
     margin .1rem
     padding .1rem 0
     text-align center
     border .02rem solid #ccc
     border-radius .06rem
 .item_list
  .item
   line-height .76rem
   padding-left .2rem
</style>
